﻿using System;
using System.Data;
using System.Data.SqlClient;
using Dtl;
namespace Dal
{
    public class ConversionDao
    {
        public static int setConversion(Conversion conversion, string ambient) 
        {
            int insert = 0;

            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return 0;
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_add_Conversion";
            cmd.Parameters.AddWithValue("@idCurrencyTo", conversion.idCurrencyTo);
            cmd.Parameters.AddWithValue("@idCurrencyFrom", conversion.idCurrencyFrom);
            cmd.Parameters.AddWithValue("@nrYear", conversion.nrYear);
            cmd.Parameters.AddWithValue("@vlConversion", conversion.vlConversion);
           

            try
            {
                cmd.Connection.Open();
                insert =  cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (SqlException exc) { 
                    Credentials.writeLogFile("setConversion: " + exc.Message);
                    return 0;
            }

            return insert;
        }


        public static DataSet getConversion(int idCurrencyTo, int idCurrencyFrom, short nrYear, string ambient)
        {
            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return null;
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Get_Conversion";
            cmd.Parameters.AddWithValue("@idCurrencyTo", idCurrencyTo);
            cmd.Parameters.AddWithValue("@idCurrencyFrom", idCurrencyFrom);
            cmd.Parameters.AddWithValue("@nrYear", nrYear);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtConversion = new DataTable("Conversiondt");

            try
            {
                cmd.Connection.Open();
                da.Fill(dtConversion);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getConversion: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("Conversionds");
            ds.Tables.Add(dtConversion);


            return ds;

        }
        public static DataSet listConversion(string ambient)
        {
            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return null;
            }
            
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_List_Conversion";           
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtConversion = new DataTable("Conversiondt");

            try
            {
                cmd.Connection.Open();
                da.Fill(dtConversion);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("listConversion: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("Conversionds");
            ds.Tables.Add(dtConversion);


            return ds;

        }

        public static int editConversion(Conversion conversion, string ambient)
        {
           
            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return 0;
            }
            int insert = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Edit_Conversion";
            cmd.Parameters.AddWithValue("@idCurrencyTo", conversion.idCurrencyTo);
            cmd.Parameters.AddWithValue("@idCurrencyFrom", conversion.idCurrencyFrom);
            cmd.Parameters.AddWithValue("@nrYear", conversion.nrYear);
            cmd.Parameters.AddWithValue("@vlConversion", conversion.vlConversion);
            

            try
            {
                cmd.Connection.Open();
                insert = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (SqlException exc) 
            {
                Credentials.writeLogFile("editConversion: " + exc.Message);
                return 0;
            }

            return insert;
        }

    }
}
